package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    /**
     *  区域分页查询
     */
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") RegionQuery qo){

        Page<Region> page = regionService.query(qo);

        model.addAttribute("page",page);

        return "region/list";
    }


    /**
     * 添加/编辑操作
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){

        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    /**
     * 查询所有目的地
     */
    @RequestMapping("listDests")
    @ResponseBody
    public Object listDests(){
        return destinationService.list();
    }


    /**
     * 查看区域关联的目的地
     * @param rid   区域id
     * @return  json格式  目的地
     */
    @RequestMapping("getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        List<Destination> destinations = destinationService.getDestByRegionId(rid);
        return destinations;
    }

    /**
     * 修改区域的热门状态
     */
    @RequestMapping("changeHotValue")
    @ResponseBody
    public Object changeHotValue( String id,int hot){

        regionService.changeHotValue(id,hot);

        return JsonResult.success();
    }

    /**
     * 约定查询目的地: 国家省份城市
     */
    @RequestMapping("getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(long deep){
        List<Destination> destinations = destinationService.getDestByDeep(deep);

        return destinations;
    }

}
